<template>
  <div class="sidebar">
    <el-menu
      class="sidebar-el-menu"
      :default-active="onRoutes"
      :collapse="collapse"
      unique-opened
      router
    >
      <!-- background-color="#324157"
      text-color="#bfcbd9"
      active-text-color="#20a0ff" -->
      <template v-for="item in items">
        <template v-if="item.subs">
          <el-sub-menu :index="item.index" :key="item.index">
            <template #title>
              <div style="padding-right: 6px">
                <el-icon :size="16">
                  <component :is="item.icon"></component>
                </el-icon>
              </div>
              <span>{{ item.title }}</span>
            </template>
            <template v-for="subItem in item.subs">
              <el-sub-menu
                v-if="subItem.subs  && subItem.level == 0"
                :index="subItem.index"
                :key="subItem.index"
              >
                <template #title>{{ subItem.title }}</template>
                <el-menu-item
                  v-for="(threeItem, i) in subItem.subs"
                  :key="i"
                  :index="threeItem.index"
                >
                  {{ threeItem.title }}</el-menu-item
                >
              </el-sub-menu>
              <el-menu-item v-else :index="subItem.index"
                >{{ subItem.title }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <template v-else>
          <el-menu-item
            :index="
              item.component?.includes('frame') ? item.component : '/home'
            "
            :key="item.index"
            @click="goLink(item.index, item.component)"
          >
            <div style="padding-right: 6px">
              <el-icon :size="16">
                <component :is="item.icon"></component>
              </el-icon>
            </div>
            <template #title>
              <div>
                {{ item.title }}
              </div>
            </template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from "vue";
import { useStore } from "vuex";
import { useRoute, useRouter } from "vue-router";
import { queryUserPermissions } from "../api/user";

let items = ref(<any>[]);
const route = useRoute();
const router = useRouter();

const menuBg = ref(<any>{
  bgColor: "#324157",
  textColor: "#bfcbd9",
  activeTextColor: "#20a0ff",
});

const onRoutes = computed(() => {
  return route.path;
});

const store = useStore();
const collapse = computed(() => store.state.collapse);

onMounted(() => {
  queryUserPermissions({}).then((res: any) => {
    if (res) {
      items.value = res.data;
    }
  });
});

const goLink = (url: string, component: string | undefined) => {
  console.log("component:", component);
  if (!url.includes("http")) {
    return;
  }
  if (component?.includes("frame")) {
    let width;
    if (url.includes("##")) {
      width = url.split("##")[1];
      url = url.split("##")[0];
    }
    // router.push({ name: "Frame", query: { url, width } });
    router.push({ path: component, query: { url } });
  } else {
    window.open(url, "_blank");
  }
};
</script>

<style>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
  background-color: #324157;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 210px;
}
.sidebar > ul {
  height: 100%;
}
</style>
